<?php

namespace app\api\controller;

use app\api\model\AdminRole as role;
use app\data\Data;
use think\Db;
use think\Request;

//
// +--------------------------------------+
// |PHP version 5.6
// +--------------------------------------+
// |管理员权限管理
// +--------------------------------------+
// |2018/01/10
// +--------------------------------------+
// |罗彪
// +--------------------------------------+
// |知果汇
// +--------------------------------------+
class AdminRole extends Index
{
    /*
     * 管理员管理权限列表接口
     * @date:2018/01/10
     * @author:罗彪
     * @parse:
            $Log:实例化日志接口
     * @return:
            $result:游客日志列表数据
     */
    public function role_list()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $where['name'] = array('like', "%$username%");

        $zgh_role = new Data('role');
        $res['data'] = $zgh_role->selectPage($page, $where);
        $res['count'] = $zgh_role->count($where);
        if (isset($res) || empty($res)) {
            $this->_jsonp('200', '权限管理数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '权限管理数据获取失败', null);
        }
    }


    public function role_add_list()
    {
        //1级菜单
        $res['data'] = db('menu')->where("pid", 0)->select();
        //2级菜单
        foreach ($res['data'] as $key => $value) {
            $children = db('menu')->where("pid = $value[id]")->select();
            $res['data'][$key]['children'] = $children;
            //3级菜单
            foreach ($children as $k => $v) {
                $child = db('menu')->where("pid = $v[id]")->select();
                $res['data'][$key]['children'][$k]['child'] = $child;
            }
        }
        $this->_jsonp('200', '成功', $res);
    }

    public function role_edit_list()
    {
        $where['id'] = trim(input('get.id'));
        $zgh_role = new Data('role');
        $res = $zgh_role->_find($where);
        //1级菜单
        $res['data'] = db('menu')->where("pid", 0)->select();
        //2级菜单
        foreach ($res['data'] as $key => $value) {
            $children = db('menu')->where("pid = $value[id]")->select();
            $res['data'][$key]['children'] = $children;
            //3级菜单
            foreach ($children as $k => $v) {
                $child = db('menu')->where("pid = $v[id]")->select();
                $res['data'][$key]['children'][$k]['child'] = $child;
            }
        }
        if ($res) {
            $this->_jsonp('200', '成功', $res);
        } else {
            $this->_jsonp('1000', '失败', null);
        }
    }

    public function role_edit()
    {
//        $id = trim(input('post.id'));
//        $info = new role;
//        $result = $info->role_edit($id);
//        return jsonp($result);
        $auth = Request::instance()->only(['name', 'desc', 'auth'], 'post');
        if (!isset($auth['auth'])) {
            $this->json('1000', '权限不能为空', null);
        }
        $data['auth'] = implode(',', $auth['auth']);
        $data['name'] = $auth['name'];
        $data['desc'] = $auth['desc'];
        $data['time'] = time();
        $id = input('post.id');
        $res = db("role")->where('id', $id)->update($data);
        if ($res) {
            action_log('编辑权限', 'log_admin', 1);
            $this->json('200', '成功', $res);
        } else {
            action_log('编辑权限', 'log_admin');
            $this->json('1000', '失败', null);
        }

    }

    public function delete()
    {
        $true = auth();
        if ($true == 2) {
            $name = trim(input('get.name'));
            if ($name == 'all') {
                $id = trim(input('get.id'));
                $where['id'] = explode(",", $id);
                for ($i = 0; $i < count($where['id']); $i++) {
                    $res = db('role')->where('id', $where['id'][$i])->delete();
                }
            } else {
                $id = trim(input('get.id'));
                $zgh_role = new Data('role');
                $res = $zgh_role->deleteDaPtaByPrimaryKey($id);
            }
//        $request = Request::instance();
//        $tabelAll = 'log_admin';
//        $where['uid'] = session('zgh_user_info');
//        $zgh_role_user = new Data('user');
//        $name = $zgh_role_user->_find($where);
//        $content = "删除角色";
//        $controller = $request->module() . '/' . $request->controller() . '/' . $request->action();
//        $ip = $request->ip();
//        $category = "正常";
//        $name = $name['username'];
//        action_log($controller, $content, $name, $category, $ip, $tabelAll);

            if ($res) {
                action_log('删除权限', 'log_admin', 1);
                $this->_jsonp('200', '成功', $res);
            } else {
                action_log('删除权限', 'log_admin');
                $this->_jsonp('1000', '失败', null);
            }

        } else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}

    }

//    public function del_all()
//    {
//        $id = trim(input('get.id'));
//        $where['id'] = explode(",", $id);
//        for ($i = 0; $i < count($where['id']); $i++) {
//            $res = db('role')->where('id', $where['id'][$i])->delete();
//        }
//        if ($res) {
//            $this->_jsonp('200', '成功', $where);
//        } else {
//            $this->_jsonp('1000', '失败', null);
//        }
//    }

    public function role_add()
    {
        $auth = Request::instance()->only(['name', 'desc', 'auth'], 'post');
        if (!isset($auth['auth'])) {
            $this->json('1000', '权限不能为空', null);
        }
        $data['auth'] = implode(',', $auth['auth']);
        $data['name'] = $auth['name'];
        $data['desc'] = $auth['desc'];
        $data['time'] = time();
        $res = Db::name("role")->insert($data);

        if ($res) {
            action_log('新增权限', 'log_admin', 1);
            $this->json('200', '成功', $res);
        } else {
            action_log('新增权限', 'log_admin');
            $this->json('1000', '失败', null);
        }

//        $info = new role;
//        $result = $info->role_add();
//        return jsonp($result);
    }
}
